<template>
  <div>
    <!-- <Spin :spinning="this.loading" tip="Loading..."> -->
    <div class="wrapper" v-loading="loading">
      <Anchor ref="anchor" :labelList="labels" :labelWidth="100">
        <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="120" :formType="type" :isSubmitBtn="false" />
        <FilterTable
          ref="table"
          height="auto"
          selectionType="single"
          :isSelectColumn="false"
          :columns="columns"
          :datakey="'rows'"
          columnsRef="baseInfo"
          :dataSource="tableList"
          :onSyncTableData="tableHandle"
          :onColumnsChange="columns => (this.columns = columns)"
          :onRowSelectChange="onRowSelectChange"
        >
          <template slot="controls">
            <el-button size="small" type="primary" icon="el-icon-plus" v-if="type !== 'show'" @click="() => this.openHandler('associatedCustomers', {})">选择关联客户</el-button>
          </template>
        </FilterTable>
      </Anchor>
    </div>
    <!-- </Spin> -->
    <BaseDialog :visible.sync="vinActions.visible" :title="vinActions.title" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerModel @close="closeHandler" @setCustomerInfo="setCustomerInfo" :formData="this.resData" />
    </BaseDialog>
    <!-- <BaseDialog :visible.sync="visible1" title="联系人信息搜索帮助" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <linkModel :ownerNo="this.ownerNo" @close="closeHandler1" @setLinkInfo="setLinkInfo" />
    </BaseDialog> -->
    <!-- <BaseDialog :visible.sync="visible2" title="产品代码搜索帮助" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <productModel @close="closeHandler2" @setProductInfo="setProductInfo" />
    </BaseDialog> -->
    <!-- <BaseDialog :visible.sync="visible3" title="颜色搜索帮助" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <colorModel :seriesId="this.seriesId" @close="closeHandler3" @setColorInfo="setColorInfo" />
    </BaseDialog> -->
    <BaseDialog :visible.sync="actions.visible" :title="actions.title" width="300" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <chooseAssociatedCustomers :resData="this.resData" @chooseAssociatedData="this.chooseAssociatedData" @close="closeAssociatedCustomers" />
    </BaseDialog>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import { isAuthorized } from '@/utils/index';
import {
  getVehicleInfoByVinAndDealerCode,
  getOwnerInfoByOwnerNoAndDealerCode,
  getServiceAdvisor,
  getChiefTechnician,
  getDcrcAdvisor,
  getInsuranceAdvisor,
  getMaintainAdvisor,
  getdiscountModeIdList,
  // getInvoiceTypeList,
  getColorList,
  getVehicleInfo,
  getRegion
} from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
import { getPinpSelect, getChexSelect, getCheModelSelect, getPackageSelect, getOptionSelect, getInteriorSelect, getExteriorSelect } from '@/api/repair/repairReception/customer/brand.js';
import { searchApAckAge } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { searchRoleByCode, searchRoleByCodes } from '@/api/common/empSelect/index';
import { findAllRepair } from '@/api/repair/basicParameters'; // 获取缺省参数
import dictCode from '@/utils/dictCode';
import * as validate from '@/utils/validate.js';
import { getUser } from '@/assets/js/auth';
import moment from 'moment';
import { queryAuthDiscountModel } from '@/api/repair/repairReception/feesSettlement';
import ownerModel from './ownerModelDealer';
import linkModel from './linkModel';
import productModel from './productModel';
import colorModel from './colorModel';
import { confirmAction, notifyAction, isFormEmpty } from '@/utils';
import { menuUserAction } from '@/api/repair/repairReception/repairBusinessManger';
import chooseAssociatedCustomers from './chooseAssociatedCustomers.vue';
import { getNameByIds } from '@/api/repair/common';

export default {
  name: 'addVehicle_baseInfo',
  components: {
    ownerModel,
    linkModel,
    productModel,
    colorModel,
    chooseAssociatedCustomers
  },
  mixins: [dictionary, authority],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    resData: {
      type: Object,
      default: {}
    }
  },
  data() {
    this.itemId = '';
    this.jumpform = null;
    this.isChange = false;
    this.seriesId = null;
    this.selectedData = null;
    this.BaseTable = null;
    return {
      formList: this.createFormList(),
      loading: false,
      labels: this.createAnchorLabels(),
      columns: this.createTableColumns(),
      actions: { visible: false, type: '', title: '' }, // 选择关联客户弹窗相关信息
      vinActions: { visible: false, type: '', title: '' }, //选择VIN弹窗相关信息
      tableList: [], //关联联系人
      regionList: [],
      baseData: {},
      id: '' // 车档id
    };
  },
  created() {
    // 初始化
    this.initialHandle();
    // 字典值查询
    this.initDictCode();
    // this.getResData();
    // this.getChiefTechnician();
    // this.getDcrcAdvisor();
    // this.getInsuranceAdvisor();
    // this.getMaintainAdvisor();
    // this.getSeries();
    // this.getdiscountModeIdList();
    // this.queryAllDiscountModeFun();
    // this.getInvoiceTypeList();
    // this.getColorList();
    // this.getProvine();
    // this.getUser();
    // this.initCheck();
  },
  mounted() {
    this.BaseTable = this.$refs.table;
    this.jumpform = document.querySelector('.scroll-wrapper');
    // this.getSystemParams();
    // this.disableVin();
  },
  methods: {
    createAnchorLabels() {
      return [
        {
          title: '车辆信息',
          id: '车辆信息'
        },
        {
          title: '车辆附加信息',
          id: '车辆附加信息'
        },
        {
          title: '车辆销售信息',
          id: '车辆销售信息'
        },
        // {
        //   title: '车辆置换信息',
        //   id: '车辆置换信息'
        // },
        {
          title: '关联客户信息',
          id: '关联客户信息'
        }
      ];
    },
    createFormList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          disabled: true
          // rules: [{ required: true, message: '请输入车牌号', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: 'VIN',
          readonly: true,
          fieldName: 'vin',
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.openHandler('selectVin', {})}></el-button>;
          },
          rules: [
            { required: true, message: '请输入VIN', trigger: 'blur' },
            { validator: validate.VIN, trigger: 'blur' }
          ],
          change: val => {
            if (val && val.length === 17) {
              this.checkRepeatVehicle(val);
            }
          },
          disabled: this.type === 'add' ? false : true
        },
        {
          type: 'INPUT',
          label: '实销审核通过日期',
          fieldName: 'salesDate',
          disabled: true,
          hidden: true
        },
        {
          type: 'INPUT',
          label: '发动机号',
          fieldName: 'engineNo',
          disabled: true
          // rules: [{ required: true, message: '请输入发动机号', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '电机号1',
          fieldName: 'motorNo1',
          disabled: true
          // rules: [{ required: true, message: '请输入发动机号', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '电机号2',
          fieldName: 'motorNo2',
          disabled: true
          // rules: [{ required: true, message: '请输入发动机号', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          disabled: true,
          itemList: [],
          change: val => {
            this.getSeries(val);
          }
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'series',
          disabled: true,
          itemList: [],
          change: val => {
            this.getModel(val);
          },
          rules: [{ required: true, message: '请选择车系', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '车型id',
          fieldName: 'model',
          disabled: true,
          itemList: [],
          hidden: true,
          change: val => {
            this.getApAckAge(val);
          },
          rules: [{ required: true, message: '请选择车型', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '车型',
          itemList: [],
          fieldName: 'modelName',
          disabled: true,
          rules: [{ required: true, message: '请选择车型', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '配置',
          fieldName: 'apAckAge',
          disabled: true,
          itemList: [],
          change: val => {
            this.getOptional(val);
          }
        },
        {
          type: 'SELECT',
          label: '选装',
          fieldName: 'optional',
          disabled: true,
          initialValue: '',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '内饰色',
          fieldName: 'innerColor',
          disabled: true,
          itemList: []
        },
        {
          type: 'SELECT',
          label: '外饰色',
          fieldName: 'outColor',
          disabled: true,
          itemList: []
        },
        {
          type: 'SELECT',
          label: '车辆性质',
          fieldName: 'businessKind',
          disabled: true,
          itemList: this.createDictList(4103)
        },
        {
          type: 'SELECT',
          label: '车辆用途',
          fieldName: 'vehiclePurpose',
          disabled: true,
          itemList: this.createDictList(1511)
        },
        {
          type: 'DATE',
          label: '生产日期',
          fieldName: 'productDate',
          valueFormat: 'yyyy-MM-dd',
          disabled: true
          // rules: [{ required: true, message: '请选择生产日期', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '销售日期',
          fieldName: 'salesDate',
          valueFormat: 'yyyy-MM-dd',
          disabled: true
          // rules: [{ required: true, message: '请选择上牌日期', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '排量/功率',
          fieldName: 'engineDesc',
          disabled: true,
          itemList: this.createDictList(6050).map(item => ({ text: item.text, value: '' + item.value }))
        },
        // {
        //   type: 'SELECT',
        //   label: '燃料类别',
        //   fieldName: 'fuelType',
        //   itemList: [],
        //   disabled: true
        // },
        {
          type: 'SELECT',
          label: '排放标准',
          fieldName: 'dischargeStandrd',
          itemList: [],
          disabled: true
        },
        // {
        //   type: 'INPUT',
        //   label: '产地',
        //   fieldName: 'productingArea',
        //   disabled: true
        // },
        {
          type: 'INPUT',
          label: '厂牌型号',
          fieldName: 'brandModel',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          disabled: true,
          itemList: this.createDictList(6008)
        },
        {
          type: 'BREAK_SPACE',
          label: '车辆附加信息'
        },
        {
          type: 'SELECT',
          label: '服务顾问',
          fieldName: 'serviceAdvisor',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '定保专员',
          fieldName: 'maintainAdvisor',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '续保专员',
          fieldName: 'insuranceAdvisor',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '客服专员',
          fieldName: 'customerService',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '指定技师',
          fieldName: 'chieftechnician',
          itemList: []
        },
        // {
        //   type: 'SELECT',
        //   label: '购买方式',
        //   fieldName: 'waysToBuy',
        //   itemList: this.createDictList(9601)
        // },
        // {
        //   type: 'SELECT',
        //   label: '优惠模式',
        //   fieldName: 'dicountway',
        //   itemList: []
        // },
        // {
        //   type: 'DATE',
        //   label: '优惠截止日期',
        //   fieldName: 'discountDate',
        //   valueFormat: 'yyyy-MM-dd'
        // },
        {
          type: 'BREAK_SPACE',
          label: '车辆销售信息'
        },
        {
          type: 'DATE',
          label: '开票日期',
          fieldName: 'invoiceDate',
          valueFormat: 'yyyy-MM-dd',
          disabled: true
        },
        {
          type: 'DATE',
          label: '上牌日期',
          fieldName: 'licenseDate',
          valueFormat: 'yyyy-MM-dd',
          disabled: true
        },
        // {
        //   type: 'DATE',
        //   label: '提车日期',
        //   fieldName: 'vehicleDate',
        //   valueFormat: 'yyyy-MM-dd',
        //   disabled: true
        // },
        {
          type: 'SELECT',
          label: '销售顾问',
          initialValue: '',
          fieldName: 'consultant',
          disabled: true,
          itemList: []
        },
        {
          type: 'SELECT',
          label: '本经销商购车',
          fieldName: 'isSelfCompany',
          initialValue: 10041002,
          itemList: this.createDictList(1004),
          disabled: false
        },
        // {
        //   type: 'INPUT',
        //   label: '营运信息',
        //   fieldName: 'operateMessage',
        //   disabled: true
        // },
        // {
        //   type: 'BREAK_SPACE',
        //   label: '车辆置换信息'
        // },
        // {
        //   type: 'DATE',
        //   label: '置换日期',
        //   fieldName: 'replaceDate',
        //   valueFormat: 'yyyy-MM-dd',
        //   disabled: true
        // },
        // {
        //   type: 'INPUT',
        //   label: '置换意向车型',
        //   fieldName: 'replaceIntentModel',
        //   disabled: true
        // },
        // {
        //   type: 'DATE',
        //   label: '再购日期',
        //   fieldName: 'rebuyDate',
        //   valueFormat: 'yyyy-MM-dd',
        //   disabled: true
        // },
        {
          type: 'BREAK_SPACE',
          label: '关联客户信息'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '客户编号',
          dataIndex: 'linkOwnerNo',
          width: 120,
          hidden: true,
          showOverFlowTip: true
        },
        {
          title: '客户姓名',
          dataIndex: 'ownerName',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '客户性别',
          dataIndex: 'gender',
          width: 90,
          showOverFlowTip: true,
          render: props => {
            return <span>{this.createDictText(props.row.gender, 8012)}</span>;
          }
        },
        {
          title: '客户电话',
          dataIndex: 'phone',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '客户手机',
          dataIndex: 'mobile',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '客户类型',
          dataIndex: 'ownerType',
          width: 120,
          showOverFlowTip: true,
          editType: 'select',
          editable: true,
          editItems: this.createDictList(4108, [41081001]),
          defaultEditable: true
        },
        {
          title: '与车主关系',
          dataIndex: 'relationship',
          width: 120,
          showOverFlowTip: true,
          editType: 'select',
          editable: true,
          editItems: this.createDictList(8127, [81271001]),
          defaultEditable: true
        },
        {
          title: '省份',
          dataIndex: 'province',
          width: 120,
          showOverFlowTip: true,
          render: props => {
            return <span>{this.getText('region', props.row.province)}</span>;
          }
        },
        {
          title: '城市',
          dataIndex: 'city',
          width: 120,
          showOverFlowTip: true,
          render: props => {
            return <span>{this.getText('region', props.row.city)}</span>;
          }
        },
        {
          title: '区\县',
          dataIndex: 'district',
          width: 120,
          showOverFlowTip: true,
          render: props => {
            return <span>{this.getText('region', props.row.district)}</span>;
          }
        },
        {
          title: '传真',
          dataIndex: 'fax',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '地址',
          dataIndex: 'address',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '邮编',
          dataIndex: 'zipCode',
          width: 120,
          showOverFlowTip: true
        },
        {
          title: '喜欢联系方式',
          dataIndex: 'bestConcatType',
          width: 120,
          showOverFlowTip: true,
          dictItems: this.createDictList('9123')
        },
        {
          title: '职业',
          dataIndex: 'vocationType',
          width: 120,
          showOverFlowTip: true,
          dictItems: this.createDictList('8022')
        },
        {
          title: '职称',
          dataIndex: 'position',
          width: 120,
          showOverFlowTip: true,
          dictItems: this.createDictList('8023')
        },
        {
          title: '备注',
          dataIndex: 'a12',
          width: 120,
          showOverFlowTip: true
        }
      ];
    },
    onRowSelectChange(val) {
      if (!val.length) {
        this.selectedData = null;
      } else {
        this.selectedData = val[0];
      }
    },
    // 取表单通用方法
    findFormItem(fieldName) {
      return this.formList.find(x => x.fieldName === fieldName) || {};
    },
    async initialHandle() {
      if (this.type === 'add') {
        this.loading = true;
        // 获取关联关系数据
        try {
          const res = await getVehicleInfo({ vin: '', linkType: 'contact', linkOwnerNo: this.resData.ownerNo });
          if (res.resultCode === 200) {
            if (res.errMsg) {
              notifyAction(res.errMsg, 'error');
            } else {
              this.tableList = res.data.rows[0].linkOwnerList;
            }
          }
          this.type === 'add' && (this.findFormItem('isSelfCompany').disabled = false); // 新建放开本经销商购车
        } catch (err) {
          console.log(err);
        } finally {
          this.loading = false;
        }
      } else {
        // 获取关联关系数据
        this.loading = true;
        try {
          const res = await getVehicleInfo({ linkType: 'contact', vin: this.resData.vin, linkVin: this.resData.vin, linkOwnerNo: this.resData.ownerNo });
          if (res.resultCode === 200) {
            if (res.errMsg) {
              notifyAction(res.errMsg, 'error');
            } else {
              this.tableList = res.data.rows[0].linkOwnerList;
            }
            res.data.rows[0].consultant && this.queryConsultantById(res.data.rows[0].consultant);
            // 表单赋值
            this.formList.forEach(x => {
              x.initialValue = res.data.rows[0][x.fieldName];
              if (
                x.fieldName == 'brand' ||
                x.fieldName == 'series' ||
                x.fieldName == 'model' ||
                x.fieldName == 'apAckAge' ||
                x.fieldName == 'optional' ||
                x.fieldName == 'innerColor' ||
                x.fieldName == 'outColor'
              ) {
                x.initialValue = res.data.rows[0][x.fieldName] + '';
              }
            });
            this.type === 'edit' && (this.findFormItem('license').disabled = false); // 编辑放开车牌号填写
          }
          // 车系车型转换
          this.getSeries(res.data.rows[0]['brand']);
          this.getModel(res.data.rows[0]['series']);
          this.getApAckAge(res.data.rows[0]['model']);
          this.getOptional(res.data.rows[0]['apAckAge']);
          this.id = res.data.rows[0]['id'];
          this.baseData = res.data.rows[0];
        } catch (err) {
          console.log(err);
        } finally {
          this.loading = false;
        }
      }
    },
    // 车牌号缺省参数
    async queryDefaultPa() {
      let res = await findAllRepair();
      this.formList.find(item => item.fieldName === 'license').initialValue = res.data['1002']; // 车牌号前缀
    },
    // table动态回调函数
    tableHandle(val, isFirst) {
      this.tableList = val;
      if (isFirst && this.BaseTable) {
        for (let i = 0; i < val.length; i++) {
          if (val[i].relationship === 81271001) {
            val[i].relationship = '车主本人';
            val[i].ownerType = '车主';
            this.BaseTable.SET_CELL_UNEDITABLE(val[i], 'relationship', true);
            this.BaseTable.SET_CELL_UNEDITABLE(val[i], 'ownerType', true);
          }
          if (this.type == 'show') {
            // 查看明细时禁用table字段编辑
            this.BaseTable.SET_CELL_UNEDITABLE(val[i], 'relationship', true);
            this.BaseTable.SET_CELL_UNEDITABLE(val[i], 'ownerType', true);
          }
        }
      }
    },
    initDictCode() {
      // 获取省市区数据
      this.getProvine();
      if (this.type === 'add') {
        // 车牌号前缀
        this.queryDefaultPa();
      }
      // 获取品牌
      this.getBrand();
      // 获取服务顾问
      this.getServiceAdvisor();
      // 获取定保专员
      this.getCommissioner();
      // 获取续保专员
      this.getInsuranceAdvisor();
      // 获取客服专员
      this.getCustomerService();
      // 获取指定技师
      this.getChieftechnician();
      // 获取内饰色
      this.getInnerColor();
      // 获取外饰色
      this.getOutColor();
    },
    // 获取省市区数据
    async getProvine() {
      const allRegion = await getRegion();
      this.regionList = allRegion.data.map(x => ({ value: x.regionId, text: x.regionName }));
    },
    // 翻译省市区
    getText(type, val) {
      if (type === 'region') {
        return this.regionList.find(x => x.value === val) ? this.regionList.find(x => x.value === val).text : '';
      }
    },
    // 服务顾问
    async getServiceAdvisor() {
      let roleParm = {
        role: dictCode.SERVICE_CONSULTANT
      };
      const res = await searchRoleByCode(roleParm);
      this.formList.find(item => item.fieldName === 'serviceAdvisor').itemList = res.data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    },
    // 获取定保专员
    async getCommissioner() {
      let roleParm = {
        role: dictCode.COMMISSIONER_FOR_SECURITY
      };
      const res = await searchRoleByCode(roleParm);
      this.formList.find(item => item.fieldName === 'maintainAdvisor').itemList = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
    },
    // 获取续保专员
    async getInsuranceAdvisor() {
      let roleParm = {
        role: dictCode.INSURANCE_SPECIALIST
      };
      const res = await searchRoleByCode(roleParm);
      this.formList.find(item => item.fieldName === 'insuranceAdvisor').itemList = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
    },
    // 获取客服专员
    async getCustomerService() {
      let roleParm = {
        role: dictCode.CUSTOMER_SERVICE_SPECIALIST
      };
      const res = await searchRoleByCodes(roleParm);
      this.formList.find(item => item.fieldName === 'customerService').itemList = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
    },
    // 获取技师
    async getChieftechnician() {
      let roleParm = {
        role: dictCode.TECHNICIAN
      };
      const res = await searchRoleByCodes(roleParm);
      this.formList.find(item => item.fieldName === 'chieftechnician').itemList = res.data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    },
    // 品牌
    async getBrand() {
      const res = await getPinpSelect();
      this.formList.find(item => item.fieldName === 'brand').itemList = res.data.map(x => ({
        value: x.brandId + '',
        text: x.brandName
      }));
    },
    // 车系
    async getSeries(val) {
      const res = await getChexSelect(val);
      this.formList.find(item => item.fieldName === 'series').itemList = res.data.map(x => ({ value: x.seriesId + '', text: x.seriesName }));
    },
    // 车型
    async getModel(seriessid) {
      const res = await getCheModelSelect(this.findFormItem('brand').initialValue, seriessid);
      this.formList.find(item => item.fieldName === 'model').itemList = res.data.map(x => ({ value: x.modelId + '', text: x.modelName, model: x.modelCode }));
    },
    // 配置
    async getApAckAge(modelsid) {
      const res = await getPackageSelect(this.findFormItem('brand').initialValue, this.findFormItem('series').initialValue, modelsid);
      this.formList.find(item => item.fieldName === 'apAckAge').itemList = res.data.map(x => ({ value: x.packageId + '', text: x.configName }));
    },
    // 选装
    async getOptional(apAckAgeId) {
      const res = await getOptionSelect(this.findFormItem('brand').initialValue, this.findFormItem('series').initialValue, this.findFormItem('model').initialValue, apAckAgeId);
      this.formList.find(item => item.fieldName === 'optional').itemList = res.data.map(x => ({ value: x.optionId + '', text: x.optionName }));
    },
    // 获取内饰色
    async getInnerColor() {
      const res = await getInteriorSelect();
      this.formList.find(item => item.fieldName === 'innerColor').itemList = res.data.map(x => ({
        value: x.colorId + '',
        text: x.colorName
      }));
    },
    // 获取外饰色
    async getOutColor() {
      const res = await getExteriorSelect();
      this.formList.find(item => item.fieldName === 'outColor').itemList = res.data.map(x => ({
        value: x.colorId + '',
        text: x.colorName
      }));
    },
    // 根据销售顾问id查询销售顾问信息
    async queryConsultantById(id) {
      let res = await getNameByIds({ userIds: id });
      if (res.resultCode == 200 && res.data.length) {
        this.formList.find(item => item.fieldName === 'consultant').itemList = [
          {
            text: res.data[0].employeeName,
            value: res.data[0].employeeId + ''
          }
        ];
      }
    },
    // 选择关联客户
    openHandler(type, val) {
      if (type === 'associatedCustomers') {
        this.ownerNo = this.resData.ownerNo;
        this.actions = { visible: true, type: 'choose', title: '选择关联客户' };
      } else if (type === 'selectVin') {
        this.resData.vin = this.findFormItem('vin').initialValue;
        this.vinActions = { visible: true, type: 'choose', title: '选择底盘号' };
      }
    },
    selectAssociatedCustomers() {},
    // 处理关联客户数据
    chooseAssociatedData(val) {
      console.log(val);
      val.linkOwnerNo = val.ownerNo;
      this.BaseTable.EXECUTE_INSERT(val);
      this.closeAssociatedCustomers();
    },
    // 关闭关联客户界面
    closeAssociatedCustomers() {
      this.actions.visible = false;
    },
    // 处理选择底盘号界面数据
    setCustomerInfo(val) {
      console.log('底盘号界面传回数据');
      console.log(val);
      // 查询销售顾问(翻译用)
      val.consultant && this.queryConsultantById(val.consultant);
      // 将传回的数据带到界面中
      this.formList.forEach(x => {
        x.initialValue = val[x.fieldName];
        if (
          x.fieldName == 'brand' ||
          x.fieldName == 'series' ||
          x.fieldName == 'model' ||
          x.fieldName == 'apAckAge' ||
          x.fieldName == 'optional' ||
          x.fieldName == 'innerColor' ||
          x.fieldName == 'outColor'
        ) {
          x.initialValue = val[x.fieldName] + '';
        }
        if (x.fieldName === 'series') {
          this.getSeries(val.brand);
        }
        if (x.fieldName === 'model') {
          this.getModel(val.series);
        }
        if (x.fieldName === 'apAckAge') {
          this.getApAckAge(val.model);
        }
        if (x.fieldName === 'optional') {
          this.getOptional(val.apAckAge);
        }
      });
      this.baseData = val;
      this.findFormItem('license').disabled = false; // 新增放开车牌号填写
      this.vinActions = { visible: false, type: 'choose', title: '选择底盘号' };
    },
    // 关联选择底盘号界面
    closeHandler() {
      this.vinActions = { visible: false, type: 'choose', title: '选择底盘号' };
    },
    async getData() {
      let [err, data1] = await this.$refs.formPanel.GET_FORM_DATA();
      let data = Object.assign(this.baseData, data1);
      data.model = this.formList.find(x => x.fieldName == 'model').initialValue;
      data.salesDate = this.formList.find(x => x.fieldName == 'salesDate').initialValue;
      data.linkList = this.tableList;
      data.ownerNo = this.resData.ownerNo;
      for (let i = 0; i < data.linkList.length; i++) {
        if (data.linkList[i].relationship === '车主本人') {
          data.linkList[i].relationship = 81271001;
          data.linkList[i].ownerType = 41081001;
        }
      }
      if (this.type === 'edit') {
        data.id = this.id;
      }
      return data;
    }
  }
};
</script>

<style lang="less" scoped>
.wrapper {
  height: calc(100vh - 135px);
}
</style>
